package com.pachong.android.baseuicomponent.adapter;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.pachong.android.baseuicomponent.view.BottomLoadStateView;

import java.util.ArrayList;
import java.util.List;

public abstract class RecyclerViewBaseAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

	private static final String TAG = RecyclerViewBaseAdapter.class.getSimpleName();
	private Context mContext;
	private List<T> mDataList = new ArrayList<T>();
	private AdapterView.OnItemClickListener mClickListener;
	private AdapterView.OnItemLongClickListener mLongClickListener;


	public RecyclerViewBaseAdapter(Context con) {
		this.mContext = con;
	}


	@Override
	public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		if (viewType == ITEM_VIEW_TYPE_HEADER) {
			return new HeaderViewHolder(mHeaderView);

		} else if (viewType == ITEM_VIEW_TYPE_LOADING) {
			return new LoadingViewHolder(mLoadingLayout);

		} else {
			return onCreateHolder(parent, viewType);
		}
	}


	public abstract RecyclerView.ViewHolder onCreateHolder(ViewGroup parent, int viewType);


	@Override
	public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
		int itemType = getItemViewType(position);
		if (itemType == ITEM_VIEW_TYPE_LOADING) {
			LoadingViewHolder loadingHolder = (LoadingViewHolder) holder;
			// loadingHolder.loadingLayout.setState(LoadState.IDLE);
		} else if (itemType == ITEM_VIEW_TYPE_HEADER) {

		} else {
			if (hasHeaderView()) {
				position--;
			}
			final int dataPosi = position;
			final long itemId = getItemId(dataPosi);

			onBindHolder(holder, position);
			holder.itemView.setTag("" + position);
			if (holder != null) {
				if (mClickListener != null) {
					holder.itemView.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							mClickListener.onItemClick(null, holder.itemView, dataPosi, itemId);

						}
					});
				}

				if (mLongClickListener != null) {
					holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {

						@Override
						public boolean onLongClick(View v) {
							return mLongClickListener
									.onItemLongClick(null, holder.itemView, dataPosi, itemId);
						}

					});
				}

			}
		}
	}


	public abstract void onBindHolder(RecyclerView.ViewHolder holder, int position);


	/**
	 * 获取 Holder 所代表的前一个View的所在的position
	 * 
	 * @param holder
	 * @return
	 */
	public int getOldPosition(RecyclerView.ViewHolder holder) {
		Object tag = holder.itemView.getTag();
		int oldPosi = -1;
		if (tag != null) {
			String tagStr = String.valueOf(tag);
			if (!TextUtils.isEmpty(tagStr)) {
				try {
					oldPosi = Integer.valueOf(tagStr);
				} catch (NumberFormatException e) {
					oldPosi = -1;
				}
			}
		}
		return oldPosi;
	}

	public static final int ITEM_VIEW_TYPE_ITEM = 0x11000001;
	public static final int ITEM_VIEW_TYPE_LOADING = 0x11000002;
	public static final int ITEM_VIEW_TYPE_HEADER = 0x11000003;


	@Override
	public int getItemViewType(int position) {
		if (position == 0 && hasHeaderView()) {
			return ITEM_VIEW_TYPE_HEADER;
		}

		if (hasLoadingLayout() && position + 1 == getItemCount()) {
			return ITEM_VIEW_TYPE_LOADING;
		}
		return ITEM_VIEW_TYPE_ITEM;
	}


	@Override
	public int getItemCount() {
		int notDataItemCount = 0;
		if (hasLoadingLayout()) {
			notDataItemCount++;
		}
		if (hasHeaderView()) {
			notDataItemCount++;
		}
		return mDataList.size() + notDataItemCount;

	}


	/**
	 * 获取有效数据的大小
	 * 
	 * @return
	 */
	public int getDataCount() {
		return mDataList.size();
	}


	public List<T> getData() {
		return mDataList;
	}


	/**
	 * 设置点击监听
	 * 注意：改方法不设置访问权限， 意味着只有同一个package的class可以访问这个方法，这个是故意这么设置的，因为我希望这个方法只能在RecyclerListViewBaseFragment中调用。
	 * 
	 * @param listener
	 */
	public void setOnItemClickListener(AdapterView.OnItemClickListener listener) {
		mClickListener = listener;
	}


	public void setOnItemLongClickListener(AdapterView.OnItemLongClickListener listener) {
		mLongClickListener = listener;
	}

	public static class LoadingViewHolder extends RecyclerView.ViewHolder {

		public BottomLoadStateView loadingLayout;


		public LoadingViewHolder(BottomLoadStateView v) {
			super(v);
			loadingLayout = v;
		}

	}

	/**
	 * Header View
	 * 
	 * @author Vivi Peng
	 *
	 */
	public static class HeaderViewHolder extends RecyclerView.ViewHolder {

		public View headerView;


		public HeaderViewHolder(View hv) {
			super(hv);
			headerView = hv;
		}

	}

	private BottomLoadStateView mLoadingLayout;
	private View mHeaderView;


	public void setLoaidngLayout(BottomLoadStateView loadingLayout) {
		mLoadingLayout = loadingLayout;
	}


	public void setHeaderView(View headerView) {
		mHeaderView = headerView;
	}


	public boolean hasHeaderView() {
		return mHeaderView == null ? false : true;
	}


	public boolean hasLoadingLayout() {
		return mLoadingLayout == null ? false : true;
	}

	public Context getContext() {
		return mContext;
	}


	public View setAdapterViewItem(ViewGroup parent, @LayoutRes int layoutId) {
		LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if (parent == null) {
			Log.w(TAG, "没有parent，adapter item可能不会match_parent");
		}
		return inflater.inflate(layoutId, parent, false);
	}

}
